package com.vipboot.platform.system.crud.service.monitor;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vipboot.framework.core.util.MapStructUtil;
import com.vipboot.framework.core.util.StringUtil;
import com.vipboot.framework.mybatisplus.core.page.PageData;
import com.vipboot.framework.mybatisplus.core.page.PageQuery;
import com.vipboot.platform.system.crud.dao.SysOperateLogDao;
import com.vipboot.platform.system.domain.bo.SysOperateLogBo;
import com.vipboot.platform.system.domain.entity.SysOperateLog;
import com.vipboot.platform.system.domain.vo.SysOperateLogVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service
public class SysOperateLogService {
    private final SysOperateLogDao sysOperateLogDao;

    /**
     * 构建QueryWrapper查询条件
     *
     * @return 查询条件
     */
    private LambdaQueryWrapper<SysOperateLog> buildQueryWrapper(SysOperateLogBo operLog) {
        Map<String, Object> params = operLog.getParams();
        return new LambdaQueryWrapper<SysOperateLog>()
                .like(StringUtil.isNotBlank(operLog.getOperIp()), SysOperateLog::getOperIp, operLog.getOperIp())
                .like(StringUtil.isNotBlank(operLog.getTitle()), SysOperateLog::getTitle, operLog.getTitle())
                .eq(operLog.getBusinessType() != null && operLog.getBusinessType() > 0,
                        SysOperateLog::getBusinessType, operLog.getBusinessType())
                .func(f -> {
                    if (ArrayUtil.isNotEmpty(operLog.getBusinessTypes())) {
                        f.in(SysOperateLog::getBusinessType, Arrays.asList(operLog.getBusinessTypes()));
                    }
                })
                .eq(operLog.getStatus() != null,
                        SysOperateLog::getStatus, operLog.getStatus())
                .like(StringUtil.isNotBlank(operLog.getOperName()), SysOperateLog::getOperName, operLog.getOperName())
                .between(params.get("beginTime") != null && params.get("endTime") != null,
                        SysOperateLog::getOperTime, params.get("beginTime"), params.get("endTime"));
    }

    public PageData<SysOperateLogVo> page(SysOperateLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOperateLog> lqw = this.buildQueryWrapper(bo);
        if (StringUtil.isBlank(pageQuery.getOrderByColumn())) {
            pageQuery.setOrderByColumn("id");
            pageQuery.setIsAsc("desc");
        }
        Page<SysOperateLogVo> page = sysOperateLogDao.pageVo(pageQuery.build(), lqw, SysOperateLogVo.class);
        return PageData.build(page);
    }

    public boolean save(SysOperateLogBo param) {
        SysOperateLog entity = MapStructUtil.convert(param, SysOperateLog.class);
        entity.setOperTime(DateUtil.date());
        return sysOperateLogDao.save(entity);
    }
}
